package beusoft.ccjk.com.calculator.java;

public class ExitCapture {
    public static void main(String... args) {
        Runtime.getRuntime().addShutdownHook(new Thread(new Runnable() {
            @Override
            public void run() {
                System.out.println("the application will be exit");
                notifyAndRelease();
            }
        }));

        int i = 0;
        while (true) {
            try {
                Thread.sleep(1000);
                System.out.println("I am working");
            } catch (Exception e) {

            }
            i++;
            if (i > 20) throw new RuntimeException("error");

        }
    }

    private static void notifyAndRelease() {
        System.out.println("notify to the admin");
        try {
            Thread.sleep(1000);
        } catch (Exception e) {
            e.printStackTrace();
        }
        System.out.println("will release resource");
        try {
            Thread.sleep(1000);
        } catch (Exception e) {
            e.printStackTrace();
        }
        System.out.println("im dead");
    }


}
